/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Control.Operacoes;
import JDBC.ConnectionFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 *
 * @author Carvalho
 */
public class UsuarioDAO{

    private Connection connection;
    private Usuario usuario;

    public UsuarioDAO() throws SQLException {
        this.connection = ConnectionFactory.getConnection();
    }

    public void inserir(Usuario obj) throws SQLException {
        usuario = obj;

        String sql1 = "insert into pessoa (nome, CEP) " +
                "            values(?,?)";

        String sql2 = "insert into pessoa_fisica (cpf, dataDeNasc, estCivil, rg, sexo, idPessoa) " +
                "            values(?, ?, ?, ?, ?)";

        String sql3 = "insert into usuarios (idPessoa_Fisica, idNivel, login, senha) " +
                "            values(?, ?, ?, ?)";

        try {
            connection.setAutoCommit(false);

            PreparedStatement stmt = connection.prepareStatement(sql1);

            stmt.setString(1, usuario.getNome());
            stmt.setString(2, String.valueOf(usuario.getCEP()));

            stmt.executeUpdate();

            stmt = connection.prepareStatement(sql2);

            stmt.setString(1, usuario.getCpf());
            stmt.setString(2, usuario.getDataNasc());
            stmt.setString(3, usuario.getEstCivil());
            stmt.setString(4, usuario.getRg());
            stmt.setString(5, usuario.getEstCivil());
            stmt.setString(6, usuario.getEstCivil());

            stmt.executeUpdate();

            /*stmt = connection.prepareStatement(sql3);

            stmt.setString(1, String.valueOf(usuario.getId()));
            stmt.setString(2, usuario.getNivel());
            stmt.setString(3, usuario.getLogin());
            stmt.setString(4, usuario.getSenha());

            stmt.executeUpdate();*/

            connection.commit();
        }catch (SQLException e) {
            System.err.println("\nError Menssage: " + e.getMessage());
            connection.rollback();
    }


    }

    public void alterar(Usuario obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void excluir(Usuario obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void consultar(Usuario obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
